package com.zxc.demo.filter;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.zxc.demo.model.ZybDcUser;
import com.zxc.demo.service.ZybDcUserService;
import com.zxc.demo.util.AESUtil;
import com.zxc.demo.util.CookieUtil;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;
import org.springframework.util.StreamUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * @Package: com.*.*.interceptor
 * @ClassName: AdminInterceptor
 * @Description:拦截器
 * @author: bzf
 */
@Log4j2
@Component
public class LoginInterceptor extends HandlerInterceptorAdapter {

    @Resource
    private ZybDcUserService zybDcUserService;


    /**
     * 在请求处理之前进行调用（Controller方法调用之前）
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
//        try {
//            //统一拦截（查询当前session是否存在user）(这里user会在每次登陆成功后，写入session)
//            if (handler instanceof HandlerMethod) {
//                HandlerMethod handlerMethod = (HandlerMethod) handler;
//                IsLogin obj = handlerMethod.getMethodAnnotation(IsLogin.class);
//                if (obj == null){
//                    return true;
//                }
//                //登录权限效验
//                Cookie cookieu = CookieUtil.getCookieByName(request,"longin");
//                if (cookieu!=null){
//                    String value = cookieu.getValue();
//                    if (!StringUtils.isEmpty(value)){
//                        String username = AESUtil.decrypt(value);
//                        ZybDcUser user = zybDcUserService.getOne(new LambdaQueryWrapper<ZybDcUser>()
//                                .eq(ZybDcUser::getUserName, username));
//                        HttpSession session=request.getSession();
//                        session.setAttribute("user", user);
//                    }else {
//                        return false;
//                    }
//                }else {
//                    return false;
//                }
//            }
//        } catch (Exception e) {
//            return false;//如果设置为false时，被请求时，拦截器执行到此处将不会继续操作
//            //如果设置为true时，请求将会继续执行后面的操作
//        }
        return true;
    }

}
